# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

# gtk3 support is still experimental
# It can be tested via _use_gtk3=yes
_use_gtk3=no
if [ "${_use_gtk3}" = "yes" ]; then
  _gtksuff=3
fi

_realname=inkscape
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.92.4
pkgrel=4
pkgdesc="Vector graphics editor using the SVG file format (mingw-w64)"
arch=('any')
url="https://inkscape.sourceforge.io/"
license=("GPL" "LGPL")
install=inkscape-${CARCH}.install
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "intltool")
depends=(
          "${MINGW_PACKAGE_PREFIX}-aspell"
          "${MINGW_PACKAGE_PREFIX}-gc"
          "${MINGW_PACKAGE_PREFIX}-ghostscript"
          "${MINGW_PACKAGE_PREFIX}-gsl"
          "${MINGW_PACKAGE_PREFIX}-gtkmm${_gtksuff}"
          "${MINGW_PACKAGE_PREFIX}-gtkspell${_gtksuff}"
          "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
          "${MINGW_PACKAGE_PREFIX}-imagemagick"
          "${MINGW_PACKAGE_PREFIX}-lcms2"
          "${MINGW_PACKAGE_PREFIX}-libcdr"
          "${MINGW_PACKAGE_PREFIX}-libvisio"
          "${MINGW_PACKAGE_PREFIX}-libxml2"
          "${MINGW_PACKAGE_PREFIX}-libxslt"
          "${MINGW_PACKAGE_PREFIX}-libwpg"
          "${MINGW_PACKAGE_PREFIX}-poppler"
          "${MINGW_PACKAGE_PREFIX}-popt"
          "${MINGW_PACKAGE_PREFIX}-potrace"
          "${MINGW_PACKAGE_PREFIX}-python2"
          $([[ "${_use_gtk3}" == "yes" ]] && echo \
            "${MINGW_PACKAGE_PREFIX}-gdl" ) \
        )
optdepends=(#"${MINGW_PACKAGE_PREFIX}-pstoedit: latex formulas"
            #"${MINGW_PACKAGE_PREFIX}-texlive-core: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-python2-numpy: some extensions"
            "${MINGW_PACKAGE_PREFIX}-python2-lxml: some extensions and filters"
            #"${MINGW_PACKAGE_PREFIX}-python2-scour: optimized SVG output"
            #"${MINGW_PACKAGE_PREFIX}-uniconvertor: reading/writing to some proprietary formats"
            )
options=('staticlibs' 'strip')
source=(https://media.inkscape.org/dl/resources/file/${_realname}-${pkgver}.tar.bz2
        inkscape-0.48.4-headers-fix.patch
        inkscape-0.48.4-python.patch
        inkscape-0.92.0-win32-relocatable.patch
        inkscape-0.48.4-python-extensions-location.patch
        inkscape-0.91-simulaneous-intltool-gettext.patch
        inkscape-0.92.3-abs-overloaded.patch
        inkscape-0.92.2-fix-imagemagick-detect.patch
        inkscape-0.92.4-poppler-0_76.patch)
sha256sums=('57ec2da8177b36614a513e2822efd73af721e690f7ddc6bd0a5fbb1525b4515e'
            'e578fbef476704a42522ec5298f0202d2e600c2e03a98dba2c62d4ee1e53b6b5'
            'ee948c90126407285b262cbea3d5a182e8fc166fddce09ea33d689fde3101a49'
            '38a880a5475dd0ad99707c18a35e716f6cd677ecb7982612f2f699002f7113bf'
            '55b0922f98c98ac5df8ba0869c46ff9eeeee9aaceef9afbaad9c4763dd3923ba'
            'ea625d1d7260d32866e4e61946fa02793b1762f66dc223364dbcc5e628bb5bfe'
            '833980a8bd2c67be66a8d7c211c7996984728372a2c55ec71e1f93e2fb2343c5'
            'aa118059aee51702af6131d95de05c4cee4e5ddfaaba5a43783c3c537c943458'
            '4becae86497871076d4b790123eb0624687e3d8509845b0d5c750a5145e8fa1f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/inkscape-0.48.4-headers-fix.patch
  patch -p1 -i ${srcdir}/inkscape-0.48.4-python.patch
  patch -p1 -i ${srcdir}/inkscape-0.92.0-win32-relocatable.patch
  patch -p1 -i ${srcdir}/inkscape-0.48.4-python-extensions-location.patch
  #patch -p1 -i ${srcdir}/inkscape-0.91-simulaneous-intltool-gettext.patch
  
  patch -p1 -i ${srcdir}/inkscape-0.92.3-abs-overloaded.patch
  #patch -p1 -i ${srcdir}/inkscape-0.92.2-fix-imagemagick-detect.patch
  patch -p1 -i ${srcdir}/inkscape-0.92.4-poppler-0_76.patch

  ./autogen.sh 
  
  sed -i 's|/usr/bin/python\>|/usr/bin/python2|g' cxxtest/*.py
  sed -i 's|/usr/bin/env python\>|/usr/bin/env python2|g' share/*/{test/,}*.py
  #sed -i 's|"python" },|"python2" },|g' src/extension/implementation/script.cpp
  sed -i 's|python -c|python2 -c|g' configure share/extensions/uniconv*.py
  sed -i '/^#include <g.kmm/i #include <glibmm.h>' src/*{,/*{,/*{,/*}}}.{h,cpp}
  sed -e "s|export-dynamic|export-all-symbols|g" -i src/Makefile.am
}

build() {
  # export lt_cv_deplibs_check_method='pass_all'
  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir -p build-${CARCH} && cd build-${CARCH}

  declare -a extra_config
  if [ "${_use_gtk3}" = "yes" ]; then
    extra_config+=(--enable-gtk3-experimental)
  fi

  CXXFLAGS+=" -std=gnu++11"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-lcms \
    --enable-poppler-cairo \
    --disable-dependency-tracking \
    --disable-strict-build \
    ${extra_config[@]}

  make
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR="${pkgdir}" install
}
